package com.itheima.health.service.impl;

import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public User findByUsername(String username) {
        //调用DAO查询用户基本信息数据
        User user = userDao.selectByUsername(username);
        //根据用户id查询用户角色
        Set<Role> roleSet=userDao.selectUserRoleByUserId(user.getId());
        //遍历角色信息  一个角色有多个权限
        for (Role role : roleSet) {
            //根据角色id查询角色所拥有得权限
            Set<Permission> permissionSet=userDao.selectRolePermissionByRoleId(role.getId());
            //将获取到得角色权限疯转到角色类中
            role.setPermissions(permissionSet);
        }
        //将角色封装到用户属性中
        user.setRoles(roleSet);
        //将查到得用户信息返回调用者
        return user;
    }
}
